package xiao.ke.sbdemo.controller;/**
 * @author XuXiaoK
 * @create 2019-11-25 20:22
 */

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import xiao.ke.sbdemo.model.Person;
import xiao.ke.sbdemo.model.Student;
import xiao.ke.sbdemo.model.User;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 *@Author XiaoKe
 *@Date 2019/11/25 20:22
 *@Description TODO
 */
@RestController
public class JsonController {

    private Map<String,Object> params = new HashMap<>();

    @RequestMapping("api/test")
    public Map<String, String> testMap() {
        HashMap<String, String> map = new HashMap<>();
        map.put("name", "xiaoke");
        map.put("age", "22");
        return map;
    }

    @RequestMapping(path = "/{city_id}/{user_id}", method = RequestMethod.GET)
    public Object findUser(@PathVariable("city_id") String cityId,
                           @PathVariable("user_id") String userId) {
        params.clear();

        params.put("cityId", cityId);
        params.put("userId", userId);

        return params;
    }

    @GetMapping(value = "/v1/page_user1")
    public Object pageUser(int from, int size) {
        params.clear();
        params.put("from", from);
        params.put("size", size);

        return params;
    }

    @GetMapping(value = "/v1/page_user2")
    public Object pageUserV2(@RequestParam(defaultValue = "0", name = "page") int from, int size) {
        params.clear();
        params.put("from", from);
        params.put("size", size);

        return params;
    }

    @RequestMapping("/v1/save_user")
    public Object saveUser(@RequestBody Person person){
        params.clear();
        params.put("person", person);
        return params;
    }

    @GetMapping(value = "/v1/get_header")
    public Object getHeader(@RequestHeader("xiaoke") String xiaoKe, String id) {
        params.clear();
        params.put("xiaoke", xiaoKe);
        params.put("id", id);

        return params;
    }

    @GetMapping("/v1/test_request")
    public Object testRequest(HttpServletRequest request){
        params.clear();
        String id = request.getParameter("id");
        params.put("id", id);
        return params;
    }

    @GetMapping("/student")
    public Object jsonStudent(){
        System.out.println("xxx");

        return new Student("xiaoke", 22);
    }


}
